package com.tutucms.common.javamail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.activation.FileDataSource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by ComXu on 2017/5/18.
 */
public class AsyncMailSender extends MailSender {
    private static final Logger log = LoggerFactory.getLogger(AsyncMailSender.class);

    private static final ExecutorService threadPool = Executors.newCachedThreadPool();

    /**
     * 默认从配置文件中创建实例
     */
    public AsyncMailSender() {
        super();
    }

    /**
     * 自定义用户创建实例
     *
     * @param smtpHostName smtp值
     * @param username     默认发件邮箱
     * @param password     密码
     * @param port         端口
     */
    public AsyncMailSender(String smtpHostName, String username, String password, String port) {
        super(smtpHostName, username, password, port);
    }

    @Override
    /**
     * 发送邮件
     *
     * @param recipient 收件人邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return 发送是否成功
     */
    public boolean send(final String recipient, final String subject, final String content) {
        try {
            submintThread(() -> AsyncMailSender.super.send(recipient, subject, content));
            return true;
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return false;
        }
    }

    /**
     * 群发邮件
     *
     * @param recipients 收件人列表
     * @param subject    主题
     * @param content    内容
     * @return 是否成功
     */
    public boolean send(final List<String> recipients, final String subject, final String content) {
        try {
            submintThread(() -> AsyncMailSender.super.send(recipients, subject, content));
            return true;
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return false;
        }
    }

    /**
     * 发送带图片的邮件
     *
     * @param recipients 收件人列表
     * @param subject    主题
     * @param content    内容
     * @param img        图片数据源
     * @return 是否成功
     */
    public boolean send(final List<String> recipients, final String subject, final String content, final FileDataSource img) {
        try {
            submintThread(() -> AsyncMailSender.super.send(recipients, subject, content, img));
            return true;
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return false;
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param recipients 收件人列表
     * @param subject    主题
     * @param file       文件
     * @param content    内容
     * @return 是否成功
     */
    public boolean send(final List<String> recipients, final String subject, final FileDataSource file, final String content) {
        try {
            submintThread(() -> AsyncMailSender.super.send(recipients, subject, file, content));
            return true;
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return false;
        }
    }

    private void submintThread(Runnable runnable) {
        threadPool.submit(runnable);
    }
}
